package TUILayer;
import ControlLayer.*;
import java.util.*;


/**
 * Write a description of class LoanUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class LoanUI
{
   
    private LoanListController loanController;
    private Scanner keyboard;
    /**
     * Constructor for objects of class LoanUI
     */
    public LoanUI()
    {
       keyboard = new Scanner(System.in);
       loanController=new LoanListController(); 
    }
    /**
     * starts the program
     */
     public void start()
    {
       printWelcomeMesssage();
       loanMenu();
    }
    
    /**
     * Loan menu.
     */
     public void loanMenu()
    {
       boolean exit = false;
       while(!exit)//that is: while exit is false
       {
          int choise = writeLoanMenu(); 
          if(choise == 1)
          {   //show person
              getLoan();
          }
          else{
              if(choise == 2){
                    createLoan();
              }  
              else{
                  if(choise == 3){
                      deleteLoan();
                  }  
                  else{
                      if(choise == 4){
                          updateLoan();
                      }
                      
                   
                          else{
                              exit = true;
                           }//end else
                        }//end else
                    }//end else
                }//end else
            }///end while
            

    }
  
    /**
     * Prints the Loan Menu.
     */
    public void printWelcomeMesssage()
    {
            System.out.println("\f *** Loan Menu ***");
            System.out.println(" (1) Show information about loan");
            System.out.println(" (2) Add loan");
            System.out.println(" (3) Delete loan (Return DVD)");
            System.out.println(" (4) Update loan");
            System.out.println(" (5) Return");
            System.out.print("\n Make your choice: ");
    }
    
    /**
     * Writes the loan menu
     */
    public int writeLoanMenu()
    {      
            
            while(true)
            {
                try{
             
     
                 
                Scanner keyboard = new Scanner(System.in);
                int choice = keyboard.nextInt();
                return choice;
               }
             
               catch(InputMismatchException e)
               {
                 System.out.println("Please use right input and try agian");
               }

            } 
      }
      
 
    /**
     * Allows the uset to set the borrow period
     */
    public int inputBorrowPeriod()
    {
        boolean ok = false;
        int input= 0;
        while(!ok)
        {
          // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Write loan period:  ");
           try{
             input = keyboard.nextInt();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("Please enter valid period");

           }
        }//end while
        return input;
    }
    
    /**
     * Allows the uset to set the state
     */
    public String intputState()
    {
    
         System.out.println(" Write borrow state:  ");
         String date = keyboard.nextLine();
         return date;
    }
    
    /**
     * Allows the uset to set the Borrow date
     */
     public String inputBorrowDate()
    {
       
         
         System.out.println(" Write borrow date:  ");
         String date = keyboard.nextLine();
         return date;
    }
    
    /**
     * Allows the uset to set the loan id.
     */
     public long inputLoanID()
    {
        boolean ok = false;
        long id = 0;
        while(!ok)
        {
        // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Write loan ID:  ");
           try{
             id = keyboard.nextLong();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("Please enter valid loan ID");
            
           }
        }//end while
        return id;
    }
    
    /**
     * Allows the uset to set the person id.
     */
    public long inputPersonID()
    {
           boolean ok = false;
        long id = 0;
        while(!ok)
        {
        // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Write person ID:  ");
           try{
             id = keyboard.nextLong();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("Please enter valid person ID");
       
           }
        }//end while
        return id;
    }
   
    /**
     * Allows the uset to set the Copy serial.
     */
    public long inputCopySerial()
    {
           boolean ok = false;
        long id = 0;
        while(!ok)
        {
        // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Write copy serail number:  ");
           try{
             id = keyboard.nextLong();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("Please enter valid copy serial");
   
           }
        }//end while
        return id;
    }
    
    /**
     * Allows the uset to set the dvdid.
     */
     public long inputDvdID()
     {
       
         boolean ok = false;
        long id = 0;
        while(!ok)
        {
        // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Write dvd ID:  ");
           try{
             id = keyboard.nextLong();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("Please enter valid dvd id");
          
           }
        }//end while
        return id;
     }
    

    /**
     * Searches for a loan.
     */
    public void getLoan()
    {
      
             long loanID=inputLoanID();
              if(!loanController.loanExist(loanID))
                {
                  System.out.println("Loan doesn't exist,please try again");
                  return;
                }
                
             try{   
             System.out.println("Borrow date: "+loanController.getDVDBorrowDate(loanID));
             System.out.println("Borrow period: "+loanController.getDVDPeriod(loanID));
          
              }
             
        
            
            catch(Exception e)
            {
               System.out.println("Wrong loan ID,please try again.");
            }

         

 
    }
    
    /**
     * Creates a loan.
     */
    public void createLoan()
    {
        printWelcomeMesssage();
        System.out.println();
        
       long dvdID=inputDvdID();
       long personID=inputPersonID();
       long copySerial=inputCopySerial();
       long loanID=inputLoanID();
       int borrowPeriod=inputBorrowPeriod();
       
       try{
           loanController.createLoan(loanID,borrowPeriod,personID,dvdID,copySerial);
           printWelcomeMesssage();
        }  
        
       catch(Exception e){
        System.out.println("Error occured while crating the loan,please try again");
        
        }
    
        
    }
    
    /**
     * Deletes a loan.
     */
    public void deleteLoan()
    {
         long loanID=inputLoanID();
         if(!loanController.loanExist(loanID))
             {
             System.out.println("Loan doesn't exist,please try again");
             return;
           }
           
       long personID=inputPersonID();
        if(!loanController.personExist(personID))
        {
           System.out.println("Person doesn't exist,please try again");
           return;
        }
        
        long dvdID=inputDvdID();
        if(!loanController.dvdExist(dvdID))
        {
          System.out.println("DVD doesn't exist,please try again");
          return;
        }
        
       long copySerial=inputCopySerial();
       if(!loanController.copyExist(dvdID,copySerial))
       {
          System.out.println("Copy doesn't exist,please try again");
          return;
        }
     
       System.out.println("Please enter after how many days the dvd is returned"); 
       int daysAfterTaken=keyboard.nextInt();
       loanController.updateDVDState(loanID,daysAfterTaken);
       System.out.println("Loan state:" + loanController.getDVDState(loanID));
       try{
       loanController.DVDisReturned(personID,dvdID,copySerial);
       
       }
       
       catch(Exception e){
        System.out.println("Error occured while deleting  the loan,illegal variables");
        
        }
    }
    
    /**
     * Updates a loan.
     */
    public void updateLoan()
    {
       long loanID=inputLoanID();
   
         if(!loanController.loanExist(loanID))
           {
             System.out.println("Loan doesn't exist,please try again");
             return;
           }
       
       System.out.println("Fill up all the new information about the selected DVD" );
       String date=inputBorrowDate();
       String state=intputState();
       int period=inputBorrowPeriod();
       
       try{
         loanController.updateLoan(loanID,date,period,state);
         printWelcomeMesssage(); 
       }
        
        catch(Exception e){
          System.out.println("Error occured while updating  the loan,illegal variables");
        
        }
        
    
    }
    

}
